package cn.hn.yh.service.ddjh;

import cn.hn.yh.common.DataStory;
import cn.hn.yh.dao.ddjh.da.MyFirstDao;
import cn.hn.yh.serviceInterface.ddjh.MyFirstServiceInf;
import cn.hn.yh.vo.Base96TVO;
import cn.hn.yh.vo.Base96VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class MyFirstService implements MyFirstServiceInf {

    @Autowired
    private MyFirstDao myFirstDao;

    @Override
    public List<String[]> getSwyd(String pdate) {
        String[] column1 = DataStory.DAY_LOAD_96COLUMN_V;
        String[] column2 = DataStory.DAY_LOAD_96COLUMN;
        String[] str = new String[96];
        String[] str_ = new String[96];
        StringBuffer str1 = new StringBuffer();
        StringBuffer str2 = new StringBuffer();
        List<String[]> list = new ArrayList<>();
        for (int i = 0; i < 96; i++) {
            str1.append(",").append(column1[i]);
            str2.append(",").append(column2[i]);
        }
//        System.out.println("pdate==="+pdate);
        Base96VO planValue = this.getPlanValue(str1.substring(1), pdate.replaceAll("-", "").substring(0, 6), pdate);
        Base96TVO realValue = this.getRealValue(str2.substring(1), pdate.replaceAll("-", "").substring(0, 6), pdate);
        if (planValue != null) {
            list.add(planValue.get96Stringlist());
        } else {
            for (int i = 0; i < 96; i++) {
                str[i] = null;
            }
            list.add(str);
        }
        if (realValue != null) {
            list.add(realValue.get96Stringlist());
        } else {
            for (int i = 0; i < 96; i++) {
                str_[i] = null;
            }
            list.add(str_);
        }
        return list;
    }

    @Override
    public String getSwyd_avg(String pdate) {
        String[] column1 = DataStory.DAY_LOAD_96COLUMN_V;
        String[] column2 = DataStory.DAY_LOAD_96COLUMN;
        DecimalFormat df = new DecimalFormat("0.0000");
        double sum = 0.0;
        int count = 0;
        String rate = "";
        Base96VO planValue = this.getPlanValue(Arrays.toString(column1).substring(1).replaceAll("]", ""), pdate.replaceAll("-", "").substring(0, 6), pdate);
        Base96TVO realValue = this.getRealValue(Arrays.toString(column2).substring(1).replaceAll("]", ""), pdate.replaceAll("-", "").substring(0, 6), pdate);
        String[] plan = planValue == null ? null : planValue.get96Stringlist();
        String[] real = realValue == null ? null : realValue.get96Stringlist();
        if (plan != null && real != null) {
            for (int i = 0; i < 96; i++) {
                boolean fcFlag = false;
                boolean hisFlag = false;
                double fc = 0.0;
                double his = 0.0;
                if (plan[i] != null && !plan[i].equals("")) {
                    fcFlag = true;
                    fc = Double.parseDouble(plan[i]);
                }
                if (real[i] != null && !real[i].equals("")) {
                    hisFlag = true;
                    his = Double.parseDouble(real[i]);
                }
                if (fcFlag && hisFlag) {
                    sum += Math.pow((fc - his) / his, 2);
                    count++;
                }
            }
            rate = df.format((1 - Math.sqrt(sum / count)) * 100);
        } else {
            rate = "--";
        }
        return rate;
    }

    public Base96VO getPlanValue(String cols, String month, String pdate) {
        return myFirstDao.getPlanValue(cols, month, pdate);
    }

    public Base96TVO getRealValue(String cols, String month, String pdate) {
        return myFirstDao.getRealValue(cols, month, pdate);
    }
}
